package com.bingfor.sharedemo;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.bingfor.sharedemo.databinding.ActivityMainBinding;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;
import java.util.List;

/**
* 分享
*@author LY
*@time 2017/6/3 13:22
* des:create file and achieve model
*/

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }


    private void initView(){
        binding.btSingleShare.setOnClickListener(v->{
            startActivity(new Intent(MainActivity.this,SingleShareActivity.class));
        });

        binding.btZidingyiShare.setOnClickListener(v -> {
//            ShareDialog shareDialog = new ShareDialog(MainActivity.this);
//            shareDialog.show();
            showDialog();
        });

        binding.btShowDialogShare.setOnClickListener(v -> {
            UMImage image = new UMImage(MainActivity.this, "http://img.xmpig.com/forum/201610/20/201816z4zvp3zov8vtpp5g.jpg");//网络图片

            new ShareAction(MainActivity.this).withText("hello").withMedia(image)
                    .setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
                    .setCallback(umShareListener).open();
        });
    }
    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            Toast.makeText(MainActivity.this,"start",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Toast.makeText(MainActivity.this,"success",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            Toast.makeText(MainActivity.this,"onError",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            Toast.makeText(MainActivity.this,"onCancel",Toast.LENGTH_SHORT).show();
        }
    };

    private void showDialog(){
        Context mContext = MainActivity.this;
        final Dialog dialog = new Dialog(mContext , R.style.Theme_Light_Dialog);
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.my_dialog,null);
        //获得dialog的window窗口
        Window window = dialog.getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        dialog.setContentView(dialogView);

        dialogView.findViewById(R.id.bt_sina).setOnClickListener(v -> {
            UMImage image = new UMImage(MainActivity.this, "http://img.xmpig.com/forum/201610/20/201816z4zvp3zov8vtpp5g.jpg");//网络图片
            new ShareAction(MainActivity.this)
                    .setPlatform(SHARE_MEDIA.SINA)
                    .withText("test")
                    .withMedia(image)
                    .setCallback(umShareListener).share();
            dialog.dismiss();
        });
        dialogView.findViewById(R.id.bt_qq).setOnClickListener(v -> {
            UMImage image = new UMImage(MainActivity.this, "http://img.xmpig.com/forum/201610/20/201816z4zvp3zov8vtpp5g.jpg");//网络图片
            new ShareAction(MainActivity.this)
                    .setPlatform(SHARE_MEDIA.QQ)
                    .withText("test")
                    .withMedia(image)
                    .setCallback(umShareListener).share();
            dialog.dismiss();
        });
        dialogView.findViewById(R.id.bt_weixin).setOnClickListener(v -> {
            UMImage image = new UMImage(MainActivity.this, "http://img.xmpig.com/forum/201610/20/201816z4zvp3zov8vtpp5g.jpg");//网络图片
            new ShareAction(MainActivity.this)
                    .setPlatform(SHARE_MEDIA.WEIXIN)
                    .withText("test")
                    .withMedia(image)
                    .setCallback(umShareListener).share();
            dialog.dismiss();
        });
        dialog.show();
    }
}
